package org.example.fastproject.web.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.fastproject.core.annotation.OperationLog;
import org.example.fastproject.core.bean.PageParam;
import org.example.fastproject.core.bean.ViewResult;
import org.example.fastproject.web.sys.model.SysUser;
import org.example.fastproject.web.sys.model.dto.user.*;
import org.example.fastproject.web.sys.model.vo.menu.MenuNode;
import org.example.fastproject.web.sys.model.vo.role.RoleVo;
import org.example.fastproject.web.sys.model.vo.user.UserVo;
import org.example.fastproject.web.sys.service.SysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 王海舟
 * @since 2023-01-16 11:48:46
 */
@RestController
@RequestMapping("/sys/sysUser")
@Api(tags = "系统：用户管理")
public class SysUserController {
    @Resource
    private SysUserService userService;

    @GetMapping("/test")
    @ApiOperation("测试")
    @PreAuthorize("hasAuthority('user.test')")
    public ViewResult test(@RequestParam(required = false) String s1,
                           @RequestParam(required = false) String s2){
        return ViewResult.success("成功");
    }

    @GetMapping("/verify")
    @ApiOperation("获取验证码")
    public ViewResult<Map<String,String>> verify(){
        Map<String,String> data = userService.verify();
        return ViewResult.success(data);
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @OperationLog("用户登录")
    public ViewResult<String> login(@RequestBody UserLoginDto dto){
        String token = userService.login(dto);
        return ViewResult.success("登录成功",token);
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public ViewResult<Long> register(@RequestBody UserRegisterDto dto){
        Long userId = userService.register(dto);
        return ViewResult.success("注册成功",userId);
    }

    @PostMapping("/logout")
    @ApiOperation("用户退出登录")
    @OperationLog("用户退出登录")
    public ViewResult logout(){
        userService.logout();
        return ViewResult.success("退出登录成功");
    }

    @GetMapping("/info")
    @ApiOperation("登录用户获取用户信息")
    public ViewResult<UserVo> info(){
        UserVo userVo = userService.info();
        return ViewResult.success(userVo);
    }

    @GetMapping("/tree")
    @ApiOperation("用户获取菜单树")
    public ViewResult<List<MenuNode>> tree(){
        List<MenuNode> tree = userService.getTree();
        return ViewResult.success(tree);
    }

    @PostMapping("/page")
    @ApiOperation("分页获取用户信息列表")
    @PreAuthorize("hasAuthority('user.page')")
    public ViewResult<Page<UserVo>> page(@RequestBody PageParam param){
        Page<UserVo> page = userService.page(param);
        return ViewResult.success(page);
    }

    @GetMapping("/{id}")
    @ApiOperation("通过id获取用户信息")
    @PreAuthorize("hasAuthority('user.id')")
    public ViewResult<SysUser> getById(@PathVariable Long id){
        SysUser sysUser = userService.getById(id);
        return ViewResult.success(sysUser);
    }

    @PostMapping("/save")
    @ApiOperation("新增用户信息")
    @PreAuthorize("hasAuthority('user.save')")
    public ViewResult<Long> save(@RequestBody UserInsertDto dto){
        Long userId = userService.save(dto);
        return ViewResult.success("注册成功",userId);
    }

    @DeleteMapping("/remove")
    @ApiOperation("删除用户")
    @PreAuthorize("hasAuthority('user.remove')")
    public ViewResult remove(@RequestParam Long id){
        boolean success = userService.remove(id);
        if(success)
            return ViewResult.success("删除成功");
        return ViewResult.error("删除失败");
    }

    @PostMapping("/update")
    @ApiOperation("更新用户信息")
    public ViewResult update(@RequestBody UserUpdateDto dto){
        boolean success = userService.update(dto);
        if(success)
            return ViewResult.success("更新成功");
        return ViewResult.error("更新失败");
    }

    @PostMapping("/role")
    @ApiOperation("给用户分配角色")
    @PreAuthorize("hasAuthority('user.role')")
    public ViewResult role(@RequestBody UserRoleDto dto){
        boolean success = userService.role(dto);
        if(success)
            return ViewResult.success("角色分配成功");
        return ViewResult.error("角色分配失败");
    }
}
